<template>
  <div class="container">
    <el-card class="container-result">
      <thy-caption title="查询结果" margin />
      <el-table size="small" stripe border :data="list" v-loading="loading">
        <el-table-column prop="id" label="ID"></el-table-column>
        <el-table-column prop="name" label="商品名称"></el-table-column>
        <el-table-column prop="desc" label="商品描述"></el-table-column>
        <el-table-column prop="pic" label="封面" width="140">
          <template slot-scope="scope">
            <img :src="scope.row.pic" alt="" width="120" />
          </template>
        </el-table-column>
        <!-- <el-table-column prop="banners" label="轮播图" width="270">
          <template slot-scope="scope">
            <img
              v-for="(item, index) in JSON.parse(scope.row.banners)"
              :key="`banner_${index}`"
              :src="item.pic"
              alt=""
              width="120"
            />
          </template>
        </el-table-column> -->
        <el-table-column prop="sale" label="原价"></el-table-column>
        <el-table-column prop="price" label="现价"></el-table-column>
        <el-table-column prop="status" label="类型">
          <template slot-scope="scope">
            <span>{{ typeList[scope.row.type] || '其它' }}</span>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="properties" label="规格"></el-table-column> -->
        <el-table-column
          prop="end"
          label="结束日期"
          width="180"
        ></el-table-column>
        <el-table-column prop="size" label="库存"></el-table-column>
        <el-table-column prop="number" label="已售"></el-table-column>
        <el-table-column prop="status" label="状态">
          <template slot-scope="scope">
            <span>{{ scope.row.status ? '下架' : '上架' }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="120" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="editHandle(scope.$index)"
              >编辑</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        @current-change="paginationHandle"
        :current-page="page"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <el-dialog title="新建商品" :visible.sync="dialogVisible" width="70%">
      <el-form
        v-if="dialogVisible"
        :model="form"
        size="mini"
        label-width="100px"
        label-suffix="："
        ref="form"
      >
        <el-form-item label="商品名称" prop="name" required>
          <el-input v-model="form.name" clearable></el-input>
        </el-form-item>
        <el-form-item label="商品描述" prop="desc" required>
          <el-input v-model="form.desc" clearable></el-input>
        </el-form-item>
        <el-form-item label="原价" prop="sale" required>
          <el-input v-model="form.sale" clearable></el-input>
        </el-form-item>
        <el-form-item label="现价" prop="price" required>
          <el-input v-model="form.price" clearable></el-input>
        </el-form-item>

        <el-form-item label="库存" prop="size" required>
          <el-input
            v-model="form.size"
            clearable
            :disabled="form.type === 2"
          ></el-input>
        </el-form-item>
        <el-form-item label="已售" prop="number" required>
          <el-input v-model="form.number" clearable></el-input>
        </el-form-item>
        <el-form-item label="封面图" prop="pic" required>
          <thy-upload v-model="form.pic"></thy-upload>
        </el-form-item>
        <el-form-item label="轮播图">
          <div class="banner-img-container">
            <div
              v-for="(item, idx) in form.banners"
              :key="`pic_${idx}`"
              class="pic-item"
            >
              <img :src="item.pic" height="100" alt="" />
              <div class="pic-opertion">
                <el-button
                  type="danger"
                  icon="el-icon-delete"
                  circle
                  @click="deletePicHandle(idx)"
                ></el-button>
              </div>
            </div>
          </div>
          <thy-upload
            @on-upload="addPicHandle"
            :showList="false"
            :limit="100"
          ></thy-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="submitForm"
          >确定
        </el-button>
        <el-button size="mini" @click="resetForm">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'flash-goods',
  data() {
    return {
      disabled: false,
      loading: true,
      dialogVisible: false,
      list: [],
      total: 0,
      page: 1,
      form: {},
      typeList: ['邮寄', '人工预约', '券码'],
      statusList: ['上架', '下架']
    };
  },
  methods: {
    paginationHandle(page) {
      this.getData(page);
    },
    getData(page = 1) {
      this.loading = true;
      this.$request
        .get('/admin/flash/goods/list', {
          params: {
            page,
            pageSize: 9999
          }
        })
        .then(data => {
          this.page = page;
          this.list = data.rows.filter(item => item.type >= 10000);
          this.total = data.count;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    addHandle() {
      this.disabled = false;
      this.form = {
        size: 0,
        number: 0,
        status: 0
      };
      this.dialogVisible = true;
    },
    editHandle(idx) {
      this.disabled = true;
      const item = JSON.parse(JSON.stringify(this.list[idx]));
      item.banners = JSON.parse(item.banners || '[]');
      this.form = item;
      this.dialogVisible = true;
    },
    submitForm() {
      this.$refs.form.validate(valid => {
        if (valid) {
          let url = '/admin/flash/goods/add';
          if (this.form.id) {
            delete this.form.created_at;
            delete this.form.updated_at;
            url = '/admin/flash/goods/update';
          }
          this.$request.post(url, this.form).then(() => {
            this.dialogVisible = false;
            this.getData();
          });
        }
      });
    },
    resetForm() {
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    addPicHandle(url) {
      this.form.banners.push({
        pic: url
      });
    },
    deletePicHandle(idx) {
      this.form.banners.splice(idx, 1);
    }
  },
  mounted() {
    this.getData();
  }
};
</script>

<style lang="less">
.banner-img-container {
  display: inline-block;
  .pic-item {
    margin-right: 10px;
    position: relative;
    display: flex;
    align-items: center;
    border: 1px solid #ccc;
    border-radius: 3px;
    float: left;
  }
  .pic-opertion {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
  }
}
</style>
